package com.supermap.business.agriculture;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dataservice.util.Table;
import com.supermap.dubbo.api.agrinfo.entity.Agrinfo;
import com.supermap.dubbo.api.agrinfo.entity.AgrinfoComment;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.agrinfo.AgrinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:农情接口层
 * @Date:18/1/29 下午2:37
 */
@Controller
@RequestMapping("AgrinfoController")
public class AgrinfoController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(AgrinfoController.class);

    @Reference(timeout = 10000)
    private AgrinfoService agrinfoService;

    /**
     * 通过用户id和区域code获得用户的农情信息
     */
    @RequestMapping("/getAgrinfoByUIdArea")
    @ResponseBody
    public JSONUtil getAgrinfoByUIdArea(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        String areaCode = request.getParameter("areaCode");
        return agrinfoService.getAgrinfoByUIdArea(userId, areaCode);
    }

    /**
     * 添加农情
     *
     * @param data
     * @return
     */
    @RequestMapping(value = "/addAgrinfo", method = RequestMethod.POST)
    @ResponseBody
    public JSONUtil addAgrinfo(@RequestBody HashMap<String, String> data) {
        String userId = data.get("userId");
        String farmlandId = data.get("farmlandId");
        String type = data.get("type");
        String growthPeriod = data.get("growthPeriod");
        String longitude = data.get("longitude");
        String latitude = data.get("latitude");
        String areaCode = data.get("areaCode");
        String descript = data.get("descript");
        String imgUrl = data.get("imgUrl");
        String development = data.get("development");

        Agrinfo agrinfo = new Agrinfo();

        agrinfo.setUserId(userId);
        agrinfo.setFarm(farmlandId);
        agrinfo.setType(type);
        agrinfo.setGrowthperiod(growthPeriod);
        agrinfo.setLatitude(latitude);
        agrinfo.setLongitude(longitude);
        agrinfo.setAreacode(areaCode);
        agrinfo.setDescript(descript);
        agrinfo.setImgUrl(imgUrl);
        agrinfo.setDevelopment(development);
        return agrinfoService.addAgrinfo(agrinfo);
    }

    /**
     * 获得所有的农情信息
     *
     * @return
     */
    @RequestMapping("/getAllAgrinfo")
    @ResponseBody
    public JSONUtil getAllAgrinfo() {

        return agrinfoService.getAllAgrinfo();
    }

    /**
     * 添加评论内容
     * @param request
     * @param agrinfoComment
     * @return
     */
    @RequestMapping("/addagrinfocomment")
    @ResponseBody
    public boolean addagrinfocomment(HttpServletRequest request, AgrinfoComment agrinfoComment){
        String uid = request.getParameter("uid");
        return agrinfoService.addagrinfocomment(agrinfoComment,uid);
    }

    /**
     * 返回该农情所有的评论
     * @param request
     * @param agrInfoId
     * @return
     */
    @RequestMapping("/getAllAgrinfoCommentByagrInfoId")
    @ResponseBody
    public JSONUtil getAllAgrinfoCommentByagrInfoId(HttpServletRequest request, int agrInfoId){
        List<AgrinfoComment> list=agrinfoService.getAllAgrinfoCommentByagrInfoId(agrInfoId);
        return agrinfoService.showAgrinfoComment(list);
    }


    /**
     * 删除农情
     * @param request
     * @return
     */
    @RequestMapping("/delAgrinfo")
    @ResponseBody
    public JSONUtil delAgrinfo(HttpServletRequest request){
        String agrinfoId = request.getParameter("agrinfoId");
        return agrinfoService.delAgrinfo(Integer.parseInt(agrinfoId));
    }

    /**
     * 根据areacode和cropid得到农情
     * @param request
     * @return
     */
    @RequestMapping("/getAgrinfoByAreacodeandCropid")
    @ResponseBody
    public JSONUtil getAgrinfoByAreacodeandCropid(HttpServletRequest request){
        String areacode = request.getParameter("areacode");
        String type = request.getParameter("cropid");
        return agrinfoService.getAgrinfoByAreacodeandCropid(areacode,type);
    }

    /**
     * 获得农情
     */
    @RequestMapping("/getAgrinfos")
    @ResponseBody
    public JSONUtil getAgrinfo(HttpServletRequest request){
        String type = request.getParameter("cropid");
        String areaCode = request.getParameter("areaCode");
        String dateTime = request.getParameter("dateTime");
        return agrinfoService.getAgrinfos(type,areaCode,dateTime);
    }
    /**
     * 根据userId获取该农情的点赞数和评论内容
     */
    @RequestMapping("/getAgrAllInfoByUserIdorAreaCode")
    @ResponseBody
    public Table getAgrAllInfoByUserIdorAreaCode(HttpServletRequest request, Table table){
        String userId=request.getParameter("userId");
        String areaCode=request.getParameter("areaCode");
        JSONUtil jsonUtil =agrinfoService.getAgrAllInfoByUserIdorAreaCode(table,userId,areaCode);
        table =(Table)jsonUtil.getData();
        table.setMsg(jsonUtil.getMsg());
        table.setCode(jsonUtil.getCode());
        return table;
    }
}
